<?php

/**
 * Content File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Content
 * @package  Admin_Models_Data_Content_Package
 * @author Johari <joharijumali@gmail.com>  
 */
	
namespace Admin\Models\Data;

use \Laravel\Database\Eloquent\Model as Eloquent;
use Datagrid;
use Laravel\Config as Config;
use Laravel\Database as DB;
use Laravel\Log;
use Laravel\Auth;
use Laravel\Str;
use Laravel\Lang;

/**
 * Data Content Model Class
 *
 * Create model class for lookup data
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Content extends Eloquent
{

	public static $header = '';
	
	/**
     * Generate Data datagrid table
	 *
	 * @return void.
     */
    public static function genListData()
	{
        $allContent = Content::paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;

        $dataname = '';
        $tableCol = DB::query('show columns from '.Content::$table);

        foreach ($tableCol as $value) {
            if ($value->field != 'created_at' && $value->field != 'updated_at' && $value->field != Content::$key) {

	            if (stristr($value->field, 'name') || stristr($value->field, 'nama')) {
                    $dataname = $value->field;
	                $title = Str::upper(Lang::line('global.data')->get());

                    $datagrid->setFields(array($value->field => $title));
             } elseif (stristr($value->field, 'desc')) {
                    $title = Str::upper(Lang::line('global.desc')->get());
                    $datagrid->setFields(array($value->field => $title));
	            }

            }
        }

        $datagrid->setAction(Lang::line('global.edit')->get(), 'editData', true, array(Content::$key));
        $datagrid->setAction(Lang::line('global.delete')->get(), 'deleteData', true, array(Content::$key, $dataname));
        $datagrid->setTable('contentGroup', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->build($allContent, Content::$key);

        return $datagrid->render();
    }
	 
	/**
     * Get Data information
     *
     * @param integer $id the integer to quote
	 *
	 * @return void.
     */
    public static function getInfo($id)
	{
        $Category = Content::find($id);

        $tableCol = DB::query('show columns from '.Content::$table);

        $id = Content::$key;
        
        $data['id'] = $Category->$id;

        foreach ($tableCol as $value) {
        	$fieldname = $value->field;
			
            if (stristr($value->field, 'name') || stristr($value->field, 'nama')) {
                $data['data'] = $Category->$fieldname;
            } elseif (stristr($value->field, 'desc')) {
        		$data['description'] = $Category->$fieldname;
            }
			
        }

        return $data;
    }
	
	/**
     * Add / Update Data information
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return void.
     */
    public static function setData($input)
	{
        $Category = ($input['id'] == null || !isset($input['id']))? new Content : Content::find($input['id']);

        $tableCol = DB::query('show columns from '.Content::$table);

        foreach ($tableCol as $value) {

        	$fieldname = $value->field;

            if (stristr($value->field, 'name') || stristr($value->field, 'nama')) {
                $Category->$fieldname = Str::title($input['data']);
            } elseif (stristr($value->field, 'desc')) {
        		$Category->$fieldname = $input['description'];
            }
			
        }

        $Category->save();

        $action = ($input['id'] == null || !isset($input['id']))? 'Insert':'<b>('.$input['id'].')</b> Update'; 

        Log::write('Data', 'Data <b>'.$input['data'].'</b> '.$action.' by <code>'.Auth::user()->username.'</code>');
    }
	
	/**
     * Removing Data Information
     *
     * @param integer $id   the integer to quote
     * @param varchar $name the varchar to quote
	 *
	 * @return void.
     */
    public static function remData($id, $name)
	{
        Content::find($id)->delete();

        Log::write('Data', 'Data Id <b>'.$name.'</b> Remove by <code>'.Auth::user()->username.'</code>');
    }

}

?>